include( 'shared.lua' )
include( 'cl_targetid.lua' )
include( 'cl_hud.lua' )
include( 'cl_help.lua' )

local CircleMat = Material( "SGM/playercircle" )

function GM:PositionScoreboard( ScoreBoard )

	ScoreBoard:SetSize( 700, ScrH() - 100 )
	ScoreBoard:SetPos( (ScrW() - ScoreBoard:GetWide()) / 2, 50 )

end

-- function fatty( pl ) --GM:OnEntityCreated( ent )

	-- if pl:IsPlayer() then
		-- pl.BuildBonePositions = PlayerBuildBonePosition
		-- pl:SetupBones()
		-- pl:InvalidateBoneCache()
	-- end

-- end

-- function PlayerBuildBonePosition( pl )

	-- local weight = pl:GetNWInt("Weight")
	-- local bone = 0 --pl:LookupBone("ValveBiped.Bip01_Pelvis")
	-- local m = pl:GetBoneMatrix(bone)
	-- m:Scale(Vector(1, 1, (weight/100)+1))
	-- pl:SetBoneMatrix(bone, m)
	
-- end

-- concommand.Add("myCC", fatty)


function GM:CalcView( ply, origin, angles, fov )
	
	if ply:GetNWBool("IsEating") or ply:GetNWBool("IsDying") then
		local view = {}
		view.origin		= origin
		view.angles		= ply:GetAngles()
		view.fov 		= fov
		
		if ( !ply.CalcView ) then
		
			ply.CalcView = {}
			
			// Try to work out the size
			local min, max = ply:WorldSpaceAABB()
			local size = max - min
			
			ply.CalcView.OffsetUp = size.z
			ply.CalcView.OffsetOut = (size.x + size.y + size.z) * 2
		
		end
		
		// Offset the origin
		local Up = view.angles:Up() * ply.CalcView.OffsetUp * 1
		local Offset = view.angles:Forward() * -ply.CalcView.OffsetOut
		
		// Trace back from the original eye position, so we don't clip through walls/objects
		local TargetOrigin = ply:GetPos() + Up + Offset
		local distance = origin - TargetOrigin
		
		local trace = {
						start = origin,
						endpos = TargetOrigin,
						filter = ply
					  }
					  
					  
		local tr = util.TraceLine( trace )
		
		view.origin = origin + tr.Normal * (distance:Length() - 30) * tr.Fraction
			
		return view
	end
	
end

function GM:DrawPlayerRing( pl )

	if ( !IsValid( pl ) ) then return end
	if ( !pl:GetNWBool( "DrawRing", false ) ) then return end
	if ( !pl:Alive() ) then return end
	
	local trace = {}
	trace.start 	= pl:GetPos() + Vector(0,0,50)
	trace.endpos 	= trace.start + Vector(0,0,-300)
	trace.filter 	= pl
	
	local tr = util.TraceLine( trace )
	
	if not tr.HitWorld then
		tr.HitPos = pl:GetPos()
	end

	local color = Color( pl:GetNWInt("Red"), pl:GetNWInt("Green"), pl:GetNWInt("Blue"))
	color.a = 40;

	render.SetMaterial( CircleMat )
	render.DrawQuadEasy( tr.HitPos + tr.HitNormal, tr.HitNormal, GAMEMODE.PlayerRingSize, GAMEMODE.PlayerRingSize, color )	

end

hook.Add( "PrePlayerDraw", "DrawPlayerRing", function( ply ) GAMEMODE:DrawPlayerRing( ply ) end ) 

function GM:HUDShouldDraw( name )

	if GAMEMODE.ScoreboardVisible then return false end

	for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudDamageIndicator", "CHudSecondaryAmmo"} do
		if name == v then return false end
	end
	
	return true
	
end